﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Category : System.Web.UI.Page
{
    DAOCategory dao_cate = new DAOCategory();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        gvCategory.DataSource = dao_cate.GetAllCategory();
        gvCategory.DataBind();
    }
    protected void gvCategory_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void txtAddCategory_Click(object sender, EventArgs e)
    {
        String CateName = txtNameCategory.Text;
        String CateDes = txtCateDescription.Text;
        Category c = new Category();
        c.CateName = CateName;
        c.CateDescription = CateDes;

        int kq = dao_cate.InsertCategory(c);
        if (kq > 0)
        {
            LoadData();
        }

    }
    protected void gvCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        // String CateId = gvCategory.Rows[e.RowIndex].Cells[0].Text;     
        Label lblId = (Label)gvCategory.Rows[e.RowIndex].FindControl("lblCateId");
        int CateId = Convert.ToInt16(lblId.Text);
        Category c = new Category();
        c.CateID = CateId;
        int kq = dao_cate.DeleteCategory(c);
        if (kq > 0)
        {
            LoadData();
        }
    }
    protected void gvCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lblId = (Label)gvCategory.Rows[e.RowIndex].FindControl("lblCateIdEdit");
        TextBox CateName= (TextBox)gvCategory.Rows[e.RowIndex].FindControl("txtCateName");
        TextBox CateDes = (TextBox)gvCategory.Rows[e.RowIndex].FindControl("txtCateEdit");
        Category c = new Category();
        c.CateID = Convert.ToInt16(lblId.Text);
        c.CateName = CateName.Text;
        c.CateDescription = CateDes.Text;

        int kq = dao_cate.UpdateCategory(c);
        if (kq > 0)
        {
            gvCategory.EditIndex = -1;
            LoadData();
        }


    }
    protected void gvCategory_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvCategory.EditIndex = e.NewEditIndex;
        LoadData();
    }
    protected void gvCategory_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvCategory.EditIndex = -1;
        LoadData();
    }
    protected void gvCategory_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvCategory.PageIndex = e.NewPageIndex;
        gvCategory.EditIndex = -1;
        LoadData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        String key = txtInputSearch.Text;
        DataSet ds = dao_cate.SearchCategoryByName(key);
        gvCategory.DataSource = ds;
        gvCategory.DataBind();

    }
}
